package com.gdk.ksoft.ui.reporters.adapters;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.constants.GoodsTypeConstant;
import com.gdk.ksoft.data.common.SystemData;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;

import java.util.HashMap;
import java.util.List;

/**
 * Created by hel on 2014-12-09.
 * 开单商品信息数据适配器
 */
public class OrderStatementDetailAdapter extends BaseAdapter {

    private Context context;
    private List<HashMap<String,Object>> list;

    public OrderStatementDetailAdapter(Context context,List<HashMap<String,Object>> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final HashMap<String,Object> item = list.get(position);
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_porderstatementdetail_lstorder,null);
            holder.imgIcon = (ImageView)convertView.findViewById(R.id.item_porderstatementdetail_lstorder_imgIcon);
            holder.txtTitle = (TextView)convertView.findViewById(R.id.item_porderstatementdetail_lstorder_txtTitle);
            holder.txtSmallTitle = (TextView)convertView.findViewById(R.id.item_porderstatementdetail_lstorder_txtSmallTitle);
            holder.txtContent = (TextView)convertView.findViewById(R.id.item_porderstatementdetail_lstorder_txtContext);
            holder.txtSmallContent = (TextView)convertView.findViewById(R.id.item_porderstatementdetail_lstorder_txtSmallContext);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        try {
            new com.lidroid.xutils.BitmapUtils(context)
                    .configDefaultLoadFailedImage(R.drawable.ic_default_goods)
                    .configDefaultLoadingImage(R.drawable.ic_default_goods)
                    .display(holder.imgIcon, SystemData.BASE_URL + item.get("gico").toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        holder.txtTitle.setText(item.get("gmc").toString());
        String goodsType = GoodsTypeConstant.getGoodsType().get(Integer.parseInt(item.get("glx").toString()));
        goodsType = goodsType.equals("正常")?"":"("+goodsType+")";
        holder.txtSmallTitle.setText(Html.fromHtml(item.get("ggg").toString()
                +"<font color='red'>"+goodsType+"</font>"));
        holder.txtContent.setText(item.get("gcode").toString());
        int piceNum = Integer.parseInt(item.get("gxsdwsl").toString());
        int singleNum = Integer.parseInt(item.get("gxsdwss").toString());
        holder.txtSmallContent.setText(
                (piceNum==0?"":piceNum+item.get("gxsdw").toString())
                        + (singleNum==0?"":singleNum+item.get("ghsdw").toString())
                +"*"+ FormatUtils.exactNumber(item.get("gdj"),"0.00") + "="
                + FormatUtils.exactNumber(item.get("gyfje"),"0.00")+"元"
        );
        return convertView;
    }

    class ViewHolder{
        public ImageView imgIcon;
        public TextView txtTitle,
                        txtSmallTitle,
                        txtContent,
                        txtSmallContent;
    }

}
